package com.deerblock.push;


import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.deerblock.Constant;
import com.deerblock.MainActivity;
import com.deerblock.R;
import com.deerblock.UmInitConfig;
import com.deerblock.utils.SPUtils;
import com.tencent.tauth.Tencent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.PushAgent;
import com.umeng.message.inapp.InAppMessageManager;
import com.umeng.message.inapp.UmengSplashMessageActivity;

public class SplashTestActivity extends UmengSplashMessageActivity {
  View inflate;
  Dialog dialog;

  private final String TAG = SplashTestActivity.class.getSimpleName();


  @Override
  public boolean onCustomPretreatment() {

    if (SPUtils.get(Constant.PolicyStatusKey, "").equals("1")) {
      /***
       *  此时初始化在appcation中进行，这里不做任何友盟初始化操作，直接跳转activity
       *
       */
      //推送全屏消息设置（非初始化操作，这是推送全屏消息设置）
      InAppMessageManager mInAppMessageManager = InAppMessageManager.getInstance(SplashTestActivity.this);
      mInAppMessageManager.setInAppMsgDebugMode(true);
      //跳转homeactivity
      mInAppMessageManager.setMainActivityPath("com.deerblock.MainActivity");
      //推送平台多维度推送决策必须调用方法(需要同意隐私协议之后初始化完成调用)
      PushAgent.getInstance(this).onAppStart();
      return super.onCustomPretreatment();
    } else {
      /*** 隐私协议授权弹窗*/
      dialog();
      return true;
    }


  }


  @SuppressLint("ResourceType")
  public void dialog() {
    dialog = new Dialog(this, R.style.dialog);
    inflate = LayoutInflater.from(SplashTestActivity.this).inflate(R.layout.dialog_layout, null);
    TextView succsebtn = (TextView) inflate.findViewById(R.id.succsebtn);
    TextView canclebtn = (TextView) inflate.findViewById(R.id.caclebtn);

    succsebtn.setOnClickListener(v -> {
      /*** uminit为1时代表已经同意隐私协议，sp记录当前状态*/
      SPUtils.put(Constant.PolicyStatusKey, "1");
      UMConfigure.submitPolicyGrantResult(getApplicationContext(), true);
      /*** 友盟sdk正式初始化*/
      UmInitConfig umInitConfig = new UmInitConfig();
      umInitConfig.UMinit(getApplicationContext());
      //关闭弹窗
      dialog.dismiss();


      //推送平台多维度推送决策必须调用方法(需要同意隐私协议之后初始化完成调用)
      PushAgent.getInstance(SplashTestActivity.this).onAppStart();

      //跳转到MainActivity
      Intent intent = new Intent(SplashTestActivity.this, MainActivity.class);
      startActivity(intent);
      finish();

    });

    canclebtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.dismiss();

        UMConfigure.submitPolicyGrantResult(getApplicationContext(), false);
        //不同意隐私协议，退出app
        android.os.Process.killProcess(android.os.Process.myPid());

      }
    });

    dialog.setContentView(inflate);
    Window dialogWindow = dialog.getWindow();
    dialogWindow.setGravity(Gravity.CENTER);

    dialog.setCancelable(false);
    dialog.show();
  }
}
